﻿#include "dialog.h"
#include "ui_dialog.h"
#include <QPushButton>
#include <QGridLayout>
#include <QLineEdit>
#include <QSignalMapper>
#include <QDesktopWidget>
#include <QDebug>
#include <QKeyEvent>
#include <QGuiApplication>
#include <QApplication>
#include <QRegExpValidator>
#include <QDBusVariant>
#include <QLoggingCategory>
#include <QAction>
#include <QMouseEvent>
#include <stdlib.h>
//#include <tesseract/baseapi.h>
//#include <leptonica/allheaders.h>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent)
    , ui(new Ui::Dialog)
{
    setWindowFlags(Qt::WindowDoesNotAcceptFocus | Qt::FramelessWindowHint |
                   Qt::WindowStaysOnTopHint | Qt::Tool | Qt::X11BypassWindowManagerHint);
    this->resize(580,380);
//    time1=startTimer(500);
    CreateMenu();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::CreateMenu()
{
    QFont font1("Sans Serif",9,50);
    QFont font2("Sans Serif",14,50);
    close_bt = new QPushButton(this);
    close_bt->setObjectName("close_bt");
    close_bt->setFont(font1);
    close_bt->setText("关闭");
    close_bt->move(510,0);
    close_bt->resize(70,40);
    close_bt->setStyleSheet("background-color:rgb(170,170,127)");
    connect(close_bt,SIGNAL(clicked()),this,SLOT(hideKeyboard()));

    delete_bt = new QPushButton(this);
    delete_bt->setObjectName("delete_bt");
    delete_bt->setFont(font1);
    delete_bt->setText("删除");
    delete_bt->move(510,50);
    delete_bt->resize(70,40);
    connect(delete_bt,SIGNAL(clicked()),this,SLOT(delete_button()));

    input_bt = new QPushButton(this);
    input_bt->setObjectName("input_bt");
    input_bt->setFont(font1);
    input_bt->move(510,120);
    input_bt->resize(70,40);
    connect(input_bt,SIGNAL(clicked()),this,SLOT(input_button()));
    //connect(input_bt,SIGNAL(clicked()),this,SLOT(buttonText_input()));

    comma_bt = new QPushButton(this);
    comma_bt->setObjectName("comma_bt");
    comma_bt->setFont(font1);
    comma_bt->move(510,170);
    comma_bt->resize(70,40);
    connect(comma_bt,SIGNAL(clicked()),this,SLOT(buttonText_input()));

    period_bt = new QPushButton(this);
    period_bt->setObjectName("period_bt");
    period_bt->setFont(font1);
    period_bt->move(510,220);
    period_bt->resize(70,40);
    connect(period_bt,SIGNAL(clicked()),this,SLOT(buttonText_input()));

    interrogation_bt = new QPushButton(this);
    interrogation_bt->setObjectName("interrogation_bt");
    interrogation_bt->setFont(font1);
    interrogation_bt->move(510,270);
    interrogation_bt->resize(70,40);
    connect(interrogation_bt,SIGNAL(clicked()),this,SLOT(buttonText_input()));

    handwriting_bt = new QPushButton(this);
    handwriting_bt->setObjectName("handwriting_bt");
    handwriting_bt->setFont(font2);
    handwriting_bt->setText("手写");
    handwriting_bt->move(0,340);
    handwriting_bt->resize(116,40);
    handwriting_bt->setStyleSheet("background-color:rgb(170,170,127)");
    connect(handwriting_bt,SIGNAL(clicked()),this,SLOT(handwriting_button()));

    number_bt = new QPushButton(this);
    number_bt->setObjectName("number_bt");
    number_bt->setFont(font2);
    number_bt->setText("123");
    number_bt->move(116,340);
    number_bt->resize(116,40);
    connect(number_bt,SIGNAL(clicked()),this,SLOT(number_button()));

    space_bt = new QPushButton(this);
    space_bt->setObjectName("space_bt");
    space_bt->setFont(font2);
    space_bt->setText("空格");
    space_bt->move(232,340);
    space_bt->resize(116,40);
    connect(space_bt,SIGNAL(clicked()),this,SLOT(space_button()));

    english_bt = new QPushButton(this);
    english_bt->setObjectName("english_bt");
    english_bt->setFont(font2);
    english_bt->setText("abc");
    english_bt->move(348,340);
    english_bt->resize(116,40);
    connect(english_bt,SIGNAL(clicked()),this,SLOT(english_button()));

    symbol_bt = new QPushButton(this);
    symbol_bt->setObjectName("symbol_bt");
    symbol_bt->setFont(font2);
    symbol_bt->setText("符号");
    symbol_bt->move(464,340);
    symbol_bt->resize(116,40);
    connect(symbol_bt,SIGNAL(clicked()),this,SLOT(symbol_button()));

    for(int i=0; i<16; i++)
    {
        symbolBt[i] = new QPushButton(this);
        symbolBt[i]->setFont(font2);
        symbolBt[i]->resize(92,40);
        symbolBt[i]->hide();
    }
    symbolBt[0]->move(40,40);
    symbolBt[1]->move(160,40);
    symbolBt[2]->move(270,40);
    symbolBt[3]->move(380,40);
    symbolBt[4]->move(40,110);
    symbolBt[5]->move(160,110);
    symbolBt[6]->move(270,110);
    symbolBt[7]->move(380,110);
    symbolBt[8]->move(40,180);
    symbolBt[9]->move(160,180);
    symbolBt[10]->move(270,180);
    symbolBt[11]->move(380,180);
    symbolBt[12]->move(40,250);
    symbolBt[13]->move(160,250);
    symbolBt[14]->move(270,250);
    symbolBt[15]->move(380,250);
    symbolBt[0]->setText("#");
    symbolBt[1]->setText("+");
    symbolBt[2]->setText("-");
    symbolBt[3]->setText("*");
    symbolBt[4]->setText("(");
    symbolBt[5]->setText(")");
    symbolBt[6]->setText("<");
    symbolBt[7]->setText(">");
    symbolBt[8]->setText(";");
    symbolBt[9]->setText(":");
    symbolBt[10]->setText("=");
    symbolBt[11]->setText("$");
    symbolBt[12]->setText("~");
    symbolBt[13]->setText("%");
    symbolBt[14]->setText("|");
    symbolBt[15]->setText("_");
    connect(symbolBt[0],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[1],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[2],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[3],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[4],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[5],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[6],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[7],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[8],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[9],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[10],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[11],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[12],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[13],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[14],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(symbolBt[15],SIGNAL(clicked()),this,SLOT(buttonText_input()));


    QString test;
    for(int i=0; i<12; i++)
    {
        numBt[i] = new QPushButton(this);
        numBt[i]->setFont(font2);
        numBt[i]->resize(92,40);
        if(i<9)
        {
            test = QString("%1").arg(i+1);
            //test.sprintf("%d",i+1);
            numBt[i]->setText(test);
        }
        numBt[i]->hide();
    }
    numBt[9]->setText("0");
    numBt[10]->setText(".");
    numBt[11]->setText("@");
    numBt[0]->move(70,60);
    numBt[1]->move(200,60);
    numBt[2]->move(330,60);
    numBt[3]->move(70,130);
    numBt[4]->move(200,130);
    numBt[5]->move(330,130);
    numBt[6]->move(70,200);
    numBt[7]->move(200,200);
    numBt[8]->move(330,200);
    numBt[9]->move(70,260);
    numBt[10]->move(200,260);
    numBt[11]->move(330,260);
    connect(numBt[0],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[1],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[2],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[3],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[4],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[5],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[6],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[7],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[8],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[9],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[10],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(numBt[11],SIGNAL(clicked()),this,SLOT(buttonText_input()));


    int x1=5,y1=55,x2=30,y2=145,x3=80,y3=235;
    for(int i=0; i<26; i++)
    {
        englishBt[i] = new QPushButton(this);
        englishBt[i]->setFont(font2);
        englishBt[i]->resize(50,50);
        if(i<10)
        {
            englishBt[i]->move(x1,y1);
            x1=x1+50;
        }
        else if(i<19)
        {
            englishBt[i]->move(x2,y2);
            x2=x2+50;
        }
        else if(i<26)
        {
            englishBt[i]->move(x3,y3);
            x3=x3+50;
        }
        englishBt[i]->hide();
    }
    englishBt[0]->setText("q");
    englishBt[1]->setText("w");
    englishBt[2]->setText("e");
    englishBt[3]->setText("r");
    englishBt[4]->setText("t");
    englishBt[5]->setText("y");
    englishBt[6]->setText("u");
    englishBt[7]->setText("i");
    englishBt[8]->setText("o");
    englishBt[9]->setText("p");
    englishBt[10]->setText("a");
    englishBt[11]->setText("s");
    englishBt[12]->setText("d");
    englishBt[13]->setText("f");
    englishBt[14]->setText("g");
    englishBt[15]->setText("h");
    englishBt[16]->setText("j");
    englishBt[17]->setText("k");
    englishBt[18]->setText("l");
    englishBt[19]->setText("z");
    englishBt[20]->setText("x");
    englishBt[21]->setText("c");
    englishBt[22]->setText("v");
    englishBt[23]->setText("b");
    englishBt[24]->setText("n");
    englishBt[25]->setText("m");
    connect(englishBt[0],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[1],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[2],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[3],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[4],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[5],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[6],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[7],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[8],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[9],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[10],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[11],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[12],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[13],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[14],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[15],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[16],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[17],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[18],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[19],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[20],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[21],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[22],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[23],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[24],SIGNAL(clicked()),this,SLOT(buttonText_input()));
    connect(englishBt[25],SIGNAL(clicked()),this,SLOT(buttonText_input()));

}

void Dialog::showKeyboard(QPoint pt, QRect focusWidget)
{
    pt.setY(pt.y() + focusWidget.height());
    QWidget::move(pt);
    ui->setupUi(this);
    QWidget::show();
    pointList.clear();
    update();
}

void Dialog::hideKeyboard()
{
    pointList.clear();
    update();
    clearData();
    biHua=1;
    lx=1;
    QWidget::hide();
}

bool Dialog::isVisible() const
{
    return QWidget::isVisible();
}

void Dialog::paintEvent(QPaintEvent *)
{
    if(handwriting)
    {
        QPainter painter(this);
        QPen pen(QBrush(QColor(0,0,0)),5);
        painter.setPen(pen);
        painter.setRenderHint(QPainter::Antialiasing,true);
        for(int i=0; i<pointList.size(); i++)
        {
            myPoint mypoint=pointList[i];
            painter.drawLine(mypoint.point.x(),mypoint.point.y(),mypoint.movePoint.x(),mypoint.point.y());
        }
    }
    else return;
}

void Dialog::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons()==Qt::LeftButton)
    {
        m_bClicked=true;
        m_Point=event->pos();
    }
}

void Dialog::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()==Qt::LeftButton&&m_bClicked)
    {
        myPoint mypoint;
        mypoint.point=event->pos();
        mypoint.movePoint=event->pos();
        pointList.append(mypoint);
        paint_event=true;
    }
    update();
}

void Dialog::mouseReleaseEvent(QMouseEvent *)
{
    if(m_bClicked)
    {
        QPixmap p = this->grab(QRect(0,0,510,340));
        QString filePathName = "a.png";
        p.save(filePathName,"png");
        system("tesseract a.png a -l chi_sim --psm 8");
        FILE *fp = fopen("a.txt","r");
        if(!fp) exit(0);

        memset(s4,0,sizeof(s4));
        strcpy(s4,s3);
        memset(s3,0,sizeof(s3));
        strcpy(s3,s2);
        memset(s2,0,sizeof(s2));
        strcpy(s2,s1);
        memset(s1,0,sizeof(s1));
        char ch;
        int i=0;
        while((ch=fgetc(fp)) != EOF)
        {
            if(ch<0)
            {
                s1[i++]=ch;
            }
        }
        s1[i]='\0';
        fclose(fp);
        if(biHua == 1)
        {
            memset(s2,0,sizeof(s2));
            memset(s3,0,sizeof(s3));
            memset(s4,0,sizeof(s4));
        }
//        QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg,true);
//        qDebug()<<"1";
        input_bt -> setText(s1);
        comma_bt -> setText(s2);
        period_bt -> setText(s3);
        interrogation_bt -> setText(s4);
        biHua++;
        system("rm -r a.png");
        system("rm -r a.txt");
    }
}

void Dialog::delete_button()
{
    delete_signals();
    pointList.clear();
    clearData();
    biHua=1;
    lx=1;
    update();
}

void Dialog::handwriting_button()
{
    handwriting_bt->setStyleSheet("background-color:rgb(170,170,127)");
    english_bt->setStyleSheet("background-color:rgb(255,255,255)");
    number_bt->setStyleSheet("background-color:rgb(255,255,255)");
    symbol_bt->setStyleSheet("background-color:rgb(255,255,255)");
    for(int i=0; i<26; i++)
    {
        englishBt[i]->hide();
    }
    for(int i=0; i<12; i++)
    {
        numBt[i]->hide();
    }
    for(int i=0; i<16; i++)
    {
        symbolBt[i]->hide();
    }
    handwriting=true;
    pointList.clear();
    update();
    paint_event=false;
    biHua=1;
    lx=1;
}

void Dialog::english_button()
{
    english_bt->setStyleSheet("background-color:rgb(170,170,127)");
    handwriting_bt->setStyleSheet("background-color:rgb(255,255,255)");
    number_bt->setStyleSheet("background-color:rgb(255,255,255)");
    symbol_bt->setStyleSheet("background-color:rgb(255,255,255)");
    for(int i=0; i<26; i++)
    {
        englishBt[i]->show();
    }
    for(int i=0; i<12; i++)
    {
        numBt[i]->hide();
    }
    for(int i=0; i<16; i++)
    {
        symbolBt[i]->hide();
    }
    handwriting=false;
    m_bClicked=false;
    pointList.clear();
    clearData();
    update();
}

void Dialog::number_button()
{
    number_bt->setStyleSheet("background-color:rgb(170,170,127)");
    handwriting_bt->setStyleSheet("background-color:rgb(255,255,255)");
    english_bt->setStyleSheet("background-color:rgb(255,255,255)");
    symbol_bt->setStyleSheet("background-color:rgb(255,255,255)");
    for(int i=0; i<12; i++)
    {
        numBt[i]->show();
    }
    for(int i=0; i<26; i++)
    {
        englishBt[i]->hide();
    }
    for(int i=0; i<16; i++)
    {
        symbolBt[i]->hide();
    }
    handwriting=false;
    m_bClicked=false;
    pointList.clear();
    clearData();
    update();
}

void Dialog::symbol_button()
{
    symbol_bt->setStyleSheet("background-color:rgb(170,170,127)");
    handwriting_bt->setStyleSheet("background-color:rgb(255,255,255)");
    english_bt->setStyleSheet("background-color:rgb(255,255,255)");
    number_bt->setStyleSheet("background-color:rgb(255,255,255)");
    for(int i=0; i<12; i++)
    {
        numBt[i]->hide();
    }
    for(int i=0; i<26; i++)
    {
        englishBt[i]->hide();
    }
    for(int i=0; i<16; i++)
    {
        symbolBt[i]->show();
    }
    handwriting=false;
    m_bClicked=false;
    pointList.clear();
    clearData();
    update();
}

void Dialog::buttonText_input()
{
    QString text = ((QPushButton*)sender())->text();
    commit(text);
}

void Dialog::input_button()
{
    lx++;
    QString text = ((QPushButton*)sender())->text();
    commit(text);
    if(lx==2)
    {
        pointList.clear();
        update();
    }
    if(lx==3)
    {
        clearData();
        lx=1;
        return;
    }
    char s0[3];
    char *arr1;
    char *arr2;
    char *arr3;
    char *arr4;
    strncpy(s0,s1,3);
    FILE *fp0 = fopen("ziku.txt","r");
//    FILE *fp0 = fopen("/opt/keyboard/ziku.txt","r");
    if(!fp0) exit(0);
//    QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg,true);
//    qDebug()<<"1";
    char buf[50];
    bool s1_lx=true,s2_lx=true,s3_lx=true,s4_lx=true;
    while(!feof(fp0))
    {
//        qDebug()<<"2";
        memset(buf,0,sizeof(buf));
        fgets(buf,sizeof(buf),fp0);
        if(strncmp(buf,s0,3)==0&&s1_lx)
        {
//            qDebug()<<"3";
            arr1=strtok(buf,s0);
            memset(s1,0,sizeof(s1));
            int i;
            for(i=0; i<3; ++i)
            {
                s1[i]=arr1[i];
            }
            s1[i]='\0';
            input_bt -> setText(s1);
            //fseek(fp0,0,SEEK_SET);
            s1_lx=false;
        }
        else if(strncmp(buf,s0,3)==0&&s2_lx)
        {
            arr2=strtok(buf,s0);
            memset(s2,0,sizeof(s2));
            int i;
            for(i=0; i<3; ++i)
            {
                s2[i]=arr2[i];
            }
            s2[i]='\0';
            comma_bt -> setText(s2);
            //fseek(fp0,0,SEEK_SET);
            s2_lx=false;
        }
        else if(strncmp(buf,s0,3)==0&&s3_lx)
        {
            arr3=strtok(buf,s0);
            memset(s3,0,sizeof(s3));
            int i;
            for(i=0; i<3; ++i)
            {
                s3[i]=arr3[i];
            }
            s3[i]='\0';
            period_bt -> setText(s3);
            s3_lx=false;
        }
        else if(strncmp(buf,s0,3)==0&&s4_lx)
        {
            arr4=strtok(buf,s0);
            memset(s4,0,sizeof(s4));
            int i;
            for(i=0; i<3; ++i)
            {
                s4[i]=arr4[i];
            }
            s4[i]='\0';
            interrogation_bt -> setText(s4);
            s4_lx=false;
        }
    }
}

void Dialog::space_button()
{
    commit(" ");
}

void Dialog::clearData()
{
    input_bt -> setText("");
    comma_bt -> setText("");
    period_bt -> setText("");
    interrogation_bt -> setText("");
    memset(s1,0,sizeof(s1));
    memset(s2,0,sizeof(s2));
    memset(s3,0,sizeof(s3));
    memset(s4,0,sizeof(s4));
}
